package com.ego.service.impl;

import com.ego.dubbo.service.ItemDubboService;
import com.ego.exception.DaoException;
import com.ego.pojo.*;
import com.ego.sender.RabbitMQSender;
import com.ego.service.ItemService;
import com.ego.utils.IDUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private RabbitMQSender rabbitMQSender;

    @Value("${ego.rabbitmq.exchange.direct}")
    private String directExchange;

    @Value("${ego.rabbitmq.queue.item-insert}")
    private String itemInsertQueue;

    @Value("${ego.rabbitmq.queue.item-delete}")
    private String itemDeleteQueue;

    @Value("${ego.rabbitmq.queue.item-reshelf}")
    private String itemReshlfQueue;

    @DubboReference
    private ItemDubboService itemDubboService;

    @Override
    public EasyUIDatagrid selectItemPage(int pageNum, int pageSize) {
        EasyUIDatagrid easyUIDatagrid = new EasyUIDatagrid();
        easyUIDatagrid.setRows(itemDubboService.selectItemByPage(pageNum, pageSize));
        easyUIDatagrid.setTotal(itemDubboService.selectItemTotal());
        return easyUIDatagrid;
    }

    @Override
    public EgoResult updateItemStatus(Long[] ids, int type) {
        try {
            int result = itemDubboService.updateStatusByIds(ids, type);
            if (type != 1) {
                //删除，下架
                rabbitMQSender.sendDirect(directExchange, itemDeleteQueue, ids);
            } else {
                //上架
                rabbitMQSender.sendDirect(directExchange, itemReshlfQueue, ids);
            }
            return EgoResult.ok();
        } catch (DaoException e) {
            e.printStackTrace();
            return EgoResult.error("更新商品状态失败");
        }
    }

    @Override
    public EgoResult saveItem(Item item, String desc, String itemParams) {
        //获取此时时间
        Date now = new Date();
        //获取一个随机ID
        long itemId = IDUtils.genItemId();
        item.setId(itemId);
        item.setUpdated(now);
        item.setCreated(now);

        ItemDesc itemDesc = new ItemDesc();
        itemDesc.setItemId(itemId);
        itemDesc.setItemDesc(desc);
        itemDesc.setUpdated(now);
        itemDesc.setUpdated(now);

        ItemParamItem itemParamItem = new ItemParamItem();
        itemParamItem.setCreated(now);
        itemParamItem.setId(IDUtils.genItemId());
        itemParamItem.setItemId(itemId);
        itemParamItem.setParamData(itemParams);
        itemParamItem.setUpdated(now);

        try {
            int i = itemDubboService.saveItem(item, itemDesc, itemParamItem);
            if (i == 1) {
                //新增商品时，向solr中同步数据
                SearchPojo searchPojo = new SearchPojo();
                searchPojo.setId(item.getId() + "");
                searchPojo.setTitle(item.getTitle());
                searchPojo.setSellPoint(item.getSellPoint());
                searchPojo.setImage(item.getImage());
                searchPojo.setPrice(item.getPrice());
                searchPojo.setDesc(itemDesc.getItemDesc());
                searchPojo.setCategoryName(item.getCid() + "");

                rabbitMQSender.sendDirect(directExchange, itemInsertQueue, searchPojo);
                return EgoResult.ok();
            }
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return EgoResult.error("新增商品失败");
    }

    @Override
    public EgoResult selectItemDesc(Long itemId) {
        ItemDesc itemDesc = itemDubboService.selectItemDescByItemId(itemId);
        if (itemDesc != null) {
            return EgoResult.ok(itemDesc);
        }
        return EgoResult.error("查询商品描述失败");
    }

    @Override
    public EgoResult updateItem(Item item, String desc, long itemParamId, String itemParams) throws DaoException {
        Date date = new Date();
        item.setUpdated(date);

        ItemDesc itemDesc = new ItemDesc();
        itemDesc.setItemId(item.getId());
        itemDesc.setItemDesc(desc);
        itemDesc.setUpdated(date);

        ItemParamItem itemParamItem = new ItemParamItem();
        itemParamItem.setUpdated(date);
        itemParamItem.setParamData(itemParams);
        itemParamItem.setId(itemParamId);
        try {
            int i = itemDubboService.updateItem(item, itemDesc, itemParamItem);
            if (i == 1) {
                //修改商品时，向solr中同步数据
                SearchPojo searchPojo = new SearchPojo();
                searchPojo.setId(item.getId() + "");
                searchPojo.setTitle(item.getTitle());
                searchPojo.setSellPoint(item.getSellPoint());
                searchPojo.setImage(item.getImage());
                searchPojo.setPrice(item.getPrice());
                searchPojo.setDesc(itemDesc.getItemDesc());
                searchPojo.setCategoryName(item.getCid() + "");

                rabbitMQSender.sendDirect(directExchange, itemInsertQueue, searchPojo);
                return EgoResult.ok();
            }
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return EgoResult.error("商品信息修改失败");
    }

}
